package com.health.ts.controller.sysmanage;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.health.ts.constant.ApiResult;
import com.health.ts.constant.CustomPage;
import com.health.ts.constant.FrontPage;
import com.health.ts.controller.BaseController;
import com.health.ts.entity.sysmanage.Msg;
import com.health.ts.service.sysmanage.MsgService;
import com.health.ts.utils.AssertUtil;
import com.health.ts.utils.GenerateIdUtil;

/**
 * 系统消息管理接口
 * @author Administrator
 */
@RestController
@RequestMapping(value = "/api/baseInfo",produces= BaseController.JSON_UTF8)
public class MessageController extends BaseController{
	
	@Autowired
	MsgService msgService;
	
	@RequestMapping(value="/saveMsg", method=RequestMethod.POST)
	public ApiResult saveMsg(@RequestBody Msg msg) {
		msg.setId(GenerateIdUtil.makeID());
		msg.setCreateTime(new Date());
		msg.setIsShow("0");//0是未发布
		msgService.save(msg);
		return ApiResult.success();
	}
	
	@RequestMapping(value="/selectMsg/{id}", method=RequestMethod.GET)
	public ApiResult selectMsg(@PathVariable String id) {
		Msg msg=msgService.getById(id);
		AssertUtil.notNull(msg, "根据消息id没有查询到消息信息");
		return ApiResult.success(msg);
	}
	
	@RequestMapping(value="/upMsg/{id}", method=RequestMethod.PUT)
	public ApiResult updateMsg(@PathVariable String id,@RequestBody Msg msg) {
		Msg info = msgService.getById(id);
		AssertUtil.notNull(info, "要修改的消息不存在！");
		msg.setId(id);
		msg.setCreateTime(new Date());
		msgService.updateById(msg);
		return ApiResult.success();
	}
	
	@RequestMapping(value="/delMsg/{id}", method=RequestMethod.DELETE)
	public ApiResult deleteMsg(@PathVariable String id) {
		msgService.removeById(id);
		return ApiResult.success();
	}
	
	//发布
	@RequestMapping(value="/showMsg/{id}", method=RequestMethod.GET)
	public ApiResult showMsg(@PathVariable String id) {
		Msg msg = msgService.getById(id);
		AssertUtil.notNull(msg, "要发布的消息不存在！");
		msg.setIsShow("1");
		msg.setShowTime(new Date());
		msgService.updateById(msg);
		return ApiResult.success();
	}
	//消息列表
	@RequestMapping(value="/listMsg", method=RequestMethod.POST)
	public ApiResult listMsg(FrontPage page) {
		QueryWrapper<Msg> QueryWrapper = new QueryWrapper<Msg>();
		String msgTitle =getParam(page,"msgTitle");
		if (StringUtils.isNotEmpty(msgTitle)){
			QueryWrapper.like("msgTitle", msgTitle); 
		}
		QueryWrapper.orderByDesc("createTime");
		Page<Msg> pageList = msgService.page(page.getPagePlus(), QueryWrapper);
		CustomPage<Msg> customPage = new CustomPage<Msg>(pageList);
		return ApiResult.success(customPage);
	}
	//统计消息数
	@RequestMapping(value="/countMsg", method=RequestMethod.GET)
	public ApiResult countMsg() {
		QueryWrapper<Msg> QueryWrapper = new QueryWrapper<Msg>();
		QueryWrapper.eq("isShow", "1");
		List<Msg> list=msgService.list(QueryWrapper);
		int count=list.size();
		return ApiResult.success(count);
	}
	
	//统计消息数
	@RequestMapping(value="/loadIds", method=RequestMethod.GET)
	public ApiResult loadIds() {
		QueryWrapper<Msg> QueryWrapper = new QueryWrapper<Msg>();
		QueryWrapper.eq("isShow", "1");
		List<Msg> list=msgService.list(QueryWrapper);
		StringBuffer Ids= new StringBuffer();
		for(int i=0;i<list.size();i++){
			Ids.append(list.get(i).getId());
			if(i<list.size()-1){
				Ids.append(",");
			}
		}
		return  ApiResult.success(Ids.toString());
	}
}
